package org.wheel.module.core.entity.vo;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.wheel.module.core.enm.NormalExceptionEnum;
import org.wheel.module.core.exception.ServiceExceptionEnum;
import org.wheel.module.core.util.Page;

import java.io.Serial;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 *  前后端统一消息定义协议 Message  之后前后端数据交互都按照规定的类型进行交互
 * {
 *   meta:{"code":code,“msg”:message}
 *   data:{....}
 * }
 * @author tomsun28
 * @date 10:48 2018/2/14
 */

@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Message implements Serializable{

    /**
	 *
	 */
	@Serial
    private static final long serialVersionUID = 1L;
    //===========================================================
    //           对象方法调用开始
    //===========================================================
    /**
     * 消息头meta 存放状态信息 code message
     */
    private MessageMeta meta;
    /**
     * 消息内容  存储实体交互数据
     */
    private Object data;
    /**
     *  禁止Message对象外部实例化
     */
    private Message() {}
    //===========================================================
    //           静态方法调用开始
    //===========================================================
    /**
     * 成功消息
     * @return Message
     */
    public static Message ok() {
    	Message msg = new Message();
    	MessageMeta meta = new MessageMeta(NormalExceptionEnum.OPERATE_SUCCESS.getCode(),
    	    NormalExceptionEnum.OPERATE_SUCCESS.getMessage(),
    	    NormalExceptionEnum.OPERATE_SUCCESS.getI18nMessage(), Boolean.TRUE);
    	msg.setMeta(meta);
    	return msg;
    }
    /**
     * 业务警告
     * @return Message
     */
    public static Message waring() {
        Message msg = new Message();
        MessageMeta meta = new MessageMeta(NormalExceptionEnum.OPERATE_WARNING.getCode(),
            NormalExceptionEnum.OPERATE_WARNING.getMessage(),
            NormalExceptionEnum.OPERATE_WARNING.getI18nMessage(), Boolean.FALSE);
        msg.setMeta(meta);
        return msg;
    }

    /**
     * 业务警告
     * @return Message
     */
    public static Message waring(int statusCode,String statusMsg) {
        Message msg = waring();
        msg.getMeta().setCode(statusCode);
        msg.getMeta().setMsg(statusMsg);
        return msg;
    }
    /**
     * 业务警告
     * @param warningMsg 警告信息
     * @return Message
     */
    public static Message waring(String warningMsg) {
        Message msg = waring();
        msg.getMeta().setMsg(warningMsg);
        return msg;
    }

    /**
     * 业务警告
     * @return Message
     */
    public static Message waring(ServiceExceptionEnum emu) {
        Message msg = waring();
        msg.getMeta().setCode(emu.getCode());
        msg.getMeta().setMsg(emu.getMessage());
        msg.getMeta().setI18nMsg(emu.getI18nMessage());
        return msg;
    }

    /**
     * 失败错误消息
     * @return Message
     */
    public static Message error() {
    	Message msg = new Message();
        MessageMeta meta = new MessageMeta(NormalExceptionEnum.OPERATE_FAIL.getCode(),
            NormalExceptionEnum.OPERATE_FAIL.getMessage(),
            NormalExceptionEnum.OPERATE_FAIL.getI18nMessage(), Boolean.FALSE);
        msg.setMeta(meta);
    	return msg;
    }
    public static Message ok(Object data) {
        Message msg = ok();
        msg.setData(data);
    	return msg;
    }
    public static Message error(String errorMsg) {
    	Message msg = error();
    	msg.getMeta().setMsg(errorMsg);
    	return msg;
    }
    
    public static Message ok(int statusCode,String statusMsg) {
        Message msg = ok();
        msg.getMeta().setCode(statusCode);
        msg.getMeta().setMsg(statusMsg);
        return msg;
    }
    public static Message error(int statusCode,String statusMsg) {
        Message msg = error();
        msg.getMeta().setCode(statusCode);
        msg.getMeta().setMsg(statusMsg);
        return msg;
    }
    public static Message ok(ServiceExceptionEnum emu) {
        Message msg = ok();
        msg.getMeta().setCode(emu.getCode());
        msg.getMeta().setMsg(emu.getMessage());
        msg.getMeta().setI18nMsg(emu.getI18nMessage());
        return msg;
    }
    public static Message error(ServiceExceptionEnum emu) {
        Message msg = error();
        msg.getMeta().setCode(emu.getCode());
        msg.getMeta().setMsg(emu.getMessage());
        msg.getMeta().setI18nMsg(emu.getI18nMessage());
        return msg;
    }

    /**
	 * 反馈message信息, 分页信息
	 * @param current
	 * @param size
	 * @param count
	 * @return
	 */
	public static Message getMessgeInfo (long current,long size,long count,long pages, Object data) {
		MessagePageData pageData = new MessagePageData(current, size, count, pages, data);
		return getMessgeInfo(pageData);
	}
	public static Message getMessgeInfo (long current,long size,long count, Object data) {
		MessagePageData pageData = new MessagePageData(current, size, count, ((count + size - 1) / size), data);
		return getMessgeInfo(pageData);
	}
	
	public static Message getMessgeInfo (MessagePageData pageData) {
	    return Message.ok(pageData);
    }
	
	
	/**
	 * 注入分页消息属性
	 * @param page
	 */
	public static MessagePageData injectPage(Page<?> page){
	    MessagePageData pageData = new MessagePageData();
	    pageData.setPageNo(page.getPageNo());
	    pageData.setPageSize(page.getPageSize());
	    pageData.setTotalCount(page.getTotalCount());
	    pageData.setTotalPage(page.getTotalPage());
	    pageData.setData(page.getList());
	    return pageData;
	}
    //===========================================================
    //           静态方法调用结束
    //===========================================================

    @JsonIgnore
    public int getMetaCode() {
        return (this.meta==null)?-1:this.meta.getCode();
    }
    @JsonIgnore
    public String getMetaMsg() {
        return (this.meta==null|| StrUtil.isBlank(this.meta.getMsg()))?"":this.meta.getMsg();
    }
    
    @SuppressWarnings("unchecked")
	public Message addData(String key, Object value) {
    	if(this.data == null) {
//    		this.data = com.google.common.collect.Maps.newHashMap();
    		this.data = new HashMap<>();
    	}
    	((Map<String, Object>)this.data).put(key, value);
    	return this;
    }
    
    public void reset() {
    	if(this.meta != null) {
    		this.meta = null;
    	}
    	if(this.data != null) {
    		this.data=null;
    	}
    }
    //===========================================================
    //           对象方法调用结束
    //===========================================================
}
